package com.example.dell.fastshopping.Trade;

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;

import com.example.dell.fastshopping.network.ApiService;
import com.example.dell.fastshopping.network.NetCallback;
import com.example.dell.fastshopping.network.RetrofitClient;
import com.example.dell.fastshopping.network.http.BaseResponse;
import com.example.dell.fastshopping.utils.SPDao;
import com.google.gson.GsonBuilder;

import java.util.HashMap;

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class TradeMoenyMolder extends ViewModel {
    private ApiService service = RetrofitClient.getInstance().create(ApiService.class);
    private MutableLiveData<BaseResponse> code = new MutableLiveData<>();
    private MutableLiveData<BaseResponse> extral = new MutableLiveData<>();
    private MutableLiveData<Object> payEnsureMonery = new MutableLiveData<>();
    private MutableLiveData<String> payOrder = new MutableLiveData<>();

    public MutableLiveData<BaseResponse> getTradeMolderCode() {
        String phone = SPDao.getUserPhone();
        RetrofitClient.execute(service.getTradeCode(phone), new NetCallback<BaseResponse>() {
            @Override
            public void netCallback(BaseResponse o) {
                code.setValue(o);
            }
        });
        return code;
    }

    public MutableLiveData<BaseResponse> extealmoney(RequestBody body) {
        RetrofitClient.execute(service.extralmoney(body), new NetCallback<BaseResponse>() {
            @Override
            public void netCallback(BaseResponse o) {
                extral.setValue(o);
            }
        });
        return extral;
    }

    public MutableLiveData<Object> payEnsureMonery(HashMap<String, Object> hashMap) {
        String s = new GsonBuilder().create().toJson(hashMap);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=UTF-8"), s);
        RetrofitClient.execute(service.rechargeEnsure(body), new NetCallback<BaseResponse<Object>>() {
            @Override
            public void netCallback(BaseResponse<Object> o) {
                if (o.isSuccess()) {
                    payEnsureMonery.setValue(o.getData());
                }
            }
        });
        return payEnsureMonery;
    }

    public LiveData<String> payOrder(HashMap<String, Object> hashMap) {

        String s = new GsonBuilder().create().toJson(hashMap);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=UTF-8"), s);
        RetrofitClient.execute(service.payOrder(body), new NetCallback<BaseResponse<String>>() {
            @Override
            public void netCallback(BaseResponse<String> o) {
                if (o.isSuccess()) {
                    payOrder.setValue(o.getData());
                }
            }
        });
        return payOrder;
    }
}
